package com.zj.seckill.util;

import org.springframework.util.DigestUtils;

/**
 * @author zhouji
 * @create 2021/4/5
 */
public class Md5Util {


    private static final String salt = "1a2b3c4d";


    public static String md5(String src){
        return DigestUtils.md5DigestAsHex(src.getBytes());
    }

    /**
     * 把用户输入的密码使用md5加密
     *
     * @param inputPass
     * @return
     */
    public static String inputPassToFormPass(String inputPass){
        String str = ""+salt.charAt(0)+salt.charAt(2) + inputPass +salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }

    /**
     * 把服务端的密码再一次加密
     * @param formPass
     * @param salt
     * @return
     */
    public static String formPassToDBPass(String formPass, String salt) {
        String str = ""+salt.charAt(0)+salt.charAt(2) + formPass +salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }

    /**
     * 整合两次加密，直接从明文密码转化为数据库所存的密码
     * @param inputPass
     * @param saltDB
     * @return
     */
    public static String inputPassToDbPass(String inputPass, String saltDB) {
        String formPass = inputPassToFormPass(inputPass);
        String dbPass = formPassToDBPass(formPass, saltDB);
        return dbPass;
    }


    public static void main(String[] args) {
        System.out.println(inputPassToDbPass("123456", "1a2b3c4d"));
    }

}
